<template>
	<view class="">
		<!--  -->
		<ben></ben>
		<!--  -->
		<view class="background_f">
			<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text"
				activeColor="#FF515E"></uni-segmented-control>
		</view>
		<view class="content">
			<view class="empty" style="margin-top: 300rpx;" v-if="list.length == 0">
				<u-empty mode="data" icon="https://cdn.uviewui.com/uview/empty/data.png">
				</u-empty>
			</view>
			<view class="" v-else>
				<view class="cartShop column pad20 min30_rl rad1" style="margin-top: 30rpx;"
					v-for="(items, index) in list" :key="index">
					<!-- 商家列表 -->
					<view class="cartName-box minbottom20">
						<image :src="items.order_product.shop_logo" class="cartName-img rad1 minright10"></image>
						<view class="omit1 size26 col3 font600" style="max-width:400rpx;">
							{{ items.order_product.shop_name }}
						</view>
						<view class="minau minright0 size24 font600" style="color: #FE3C3C;"
							v-if="items.order_status == 1">
							待支付
						</view>
						<view class="minau minright0 size24 font600" style="color: #FE3C3C;"
							v-if="items.order_status == 403">
							支付超时
						</view>
						<view class="minau minright0 size24 font600" style="color: #FE3C3C;"
							v-if="items.order_status == 2">
							待核销
						</view>
						<view class="minau minright0 size24 font600" style="color: #FE3C3C;"
							v-if="items.order_status == 3">
							已完成
						</view>
						<view class="minau minright0 size24 font600" style="color: #FE3C3C;"
							v-if="items.order_status == 4">
							审核中
						</view>
						<view class="minau minright0 size24 font600" style="color: #FE3C3C;"
							v-if="items.order_status == 5">
							已取消
						</view>

					</view>
					<!-- 商品列表 -->
					<view class="row" style="margin-bottom: 20rpx;"
						@click="$jump.nav('/mypackage/user/order_info?id=' + items.id + '&order_no=' + items.order_no + '&total_price=' + items.shop_total_price + '&create_time=' + items.create_time)">
						<view class="rad1">
							<image :src="items.order_product.goods_cover" class="cartDetail-imgs rad1"></image>
						</view>
						<view class="column minleft10 flex1">
							<view class="size28 col3 font600 omit1">{{ items.order_product.goods_name }}</view>
							<view class="size24 col9 font500 mintop10">
								规格：{{ items.order_product.specs_name }}
							</view>
							<view class="cartNum minto minbottom0">
								<view class="minau minright0">
									<u-number-box v-model="items.order_product.specs_num" :disabled="true"
										buttonSize="22" @change="valChange"></u-number-box>
								</view>
							</view>
						</view>
					</view>
					<view class="row padtop20" style="border-top: 2rpx solid #f6f6f6;">
						<view class="column col6 size24 font500">
							共计{{ items.total_specs_num }}件商品
							<view class="row mintop10" style="align-items: baseline;">
								合计:
								<view class="size28" style="color: #FE3C3C;">
									￥{{ items.total_price }}
								</view>
							</view>
						</view>
						<view class="minau minright0 disflex">
							<view class="minto minbottom0">
								<u-button text="去支付" shape="circle"
									color="linear-gradient( 319deg, #FF612F 4%, #FFB800 100%)" size="small"
									customStyle="width: 160rpx;" v-if="items.order_status == 1"
									@click="onNav(items)"></u-button>

								<u-button text="取货码" shape="circle"
									color="linear-gradient( 319deg, #FF612F 4%, #FFB800 100%)" size="small"
									customStyle="width: 160rpx;" v-if="items.order_status == 2"
									@click="$jump.nav('/mypackage/user/take_goods?id=' + items.id + '&order_no=' + items.order_no + '&total_price=' + items.shop_total_price + '&create_time=' + items.create_time + '&code=' + items.verification_code)"></u-button>

								<u-button text="删除订单" shape="circle"
									color="linear-gradient( 319deg, #FF612F 4%, #FFB800 100%)" size="small"
									customStyle="width: 160rpx;" v-if="items.order_status == 403"
									@click="getOrderDel(items.id)"></u-button>
								<!-- <u-button text="申请售后" shape="circle" plain="true" color="#666" size="small"
									customStyle="width: 160rpx;" v-if="items.order_status == 3"
									@click="$jump.nav('/mypackage/user/return_goods?id=' + items.id + '&order_no=' + items.order_no + '&total_price=' + items.deducted_order_total_price + '&create_time=' + items.create_time)"></u-button> -->

								<!-- <u-button text="撤销退款" shape="circle"
									color="linear-gradient( 319deg, #FF612F 4%, #FFB800 100%)" size="small"
									customStyle="width: 160rpx;" v-if="items.order_status == 4"></u-button> -->
							</view>
						</view>
					</view>
				</view>
				<view style="padding: 40rpx 0;"><u-loadmore :status="status" /></view>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				items: ['全部', '待付款', '待核销', '已完成', '已取消'],
				current: 0,
				user_id: '',
				order_status: '',
				page: 1,
				status: 'nomore',
				loadmore: [],
				list: []
			}
		},
		onLoad(op) {
			this.current = op.type
		},
		onShow() {
			//当前页面展示的时候他就执行该生命周期
			this.user_id = uni.getStorageSync('user_id');
			const token = uni.getStorageSync('token');
			if (token) {
				//如果存在token
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 10 // 滚动动画时长，默认300ms
				});
				this.page = 1
				if (this.current == 0) {
					this.order_status = ''
					this.getOrderList()
				} else if (this.current == 1) {
					this.order_status = 1
					this.getOrderList()
				} else if (this.current == 2) {
					this.order_status = 2
					this.getOrderList()
				} else if (this.current == 3) {
					this.order_status = 3
					this.getOrderList()
				} else if (this.current == 4) {
					this.order_status = 5
					this.getOrderList()
				}
				//按正常逻辑来讲 登录成功 我们是不是需要向服务器获取用户的信息？
				//那么我们一起来获取用户信息
			} else {

				uni.showToast({
					title: '您还未登录，正在前往登录',
					icon: 'none'
				})
				setTimeout(function() {
					uni.navigateTo({
						url: "/mypackage/user/login"
					})
				}, 1000)
			}
		},
		onReachBottom() {
			if (this.page < 0) return;
			this.status = 'loading';
			this.page = ++this.page
			this.getOrderList()
			setTimeout(() => {
				console.log(this.loadmore);
				if (this.loadmore.length == 0) this.status = 'nomore';
				else this.status = 'loading';
			}, 1000)
		},
		methods: {
			async getOrderList() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				let {
					code,
					message,
					data
				} = await this.$com.fetchGet("member/order/" + this.user_id, {
					order_status: this.order_status,
					page: this.page
				})
				if (code == 200) {
					uni.hideLoading()
					if (this.page != 1) {
						this.loadmore = data.items
						for (var i = 0; i < data.items.length; i++) {
							this.list.push(data.items[i]); // 加载的数据追加在上一页后面
						}
					} else {
						this.loadmore = data.items
						this.list = data.items
					}
				} else if (code == 500) {
					this.list = data
					this.loadmore = data
				} else {
					console.log(message);
				}
			},
			async getOrderDel(id) {
				console.log(id);
				let {
					code,
					message,
					data
				} = await this.$com.fetchDel("member/order_del/" + id)
				if (code == 200) {
					uni.showToast({
						icon: "success",
						title: message
					})
					setTimeout(() => {
						this.page = 1
						this.getOrderList()
					}, 1000)
				} else {
					uni.showToast({
						icon: "error",
						title: message
					})
				}
			},
			onNav(items) {
				var username = items.order_product.address_name
				var phone = items.order_product.address_phone
				var all = []
				console.log(items);
				let cartarr = {}
				cartarr["stores_name"] = items.order_product.shop_name
				cartarr["stores_logo"] = items.order_product.shop_logo
				cartarr["products"] = []
				let arr = {}
				let specs = {}
				arr["image"] = items.order_product.goods_cover
				arr["title"] = items.order_product.goods_name
				specs["name"] = items.order_product.specs_name
				specs["selling_price"] = items.order_product.specs_price
				specs["specs_num"] = items.order_product.specs_num
				arr["specs"] = specs
				cartarr["products"].push(arr)
				all.push(cartarr)
				uni.navigateTo({
					url: '/indexpackage/user/place_order?list=' + JSON.stringify(all) + '&order_id=' + items.id +
						'&username=' + username + '&phone=' + phone + '&total_price=' + items
						.total_price + '&totalintegral=' + items
						.points_available
				});
			},
			onClickItem(e) {
				if (this.current != e.currentIndex) {
					this.current = e.currentIndex;
				}
				this.page = 1
				this.list = []
				this.status = 'loadmore'
				if (this.current == 0) {
					this.order_status = ''
					this.getOrderList()
				} else if (this.current == 1) {
					this.order_status = 1
					this.getOrderList()
				} else if (this.current == 2) {
					this.order_status = 2
					this.getOrderList()
				} else if (this.current == 3) {
					this.order_status = 3
					this.getOrderList()
				} else if (this.current == 4) {
					this.order_status = 4
					this.getOrderList()
				}
			},
		}
	}
</script>

<style scoped lang="less">
	.background_f {
		position: fixed;
		top: 0;
		width: 100%;
		z-index: 5;
	}

	.content {
		margin-top: 100rpx;

		.cartShop {
			box-shadow: 0 0 9rpx 3rpx #ccc;
			background-color: white;
		}

		.cartName {
			font-size: 29rpx;
			color: #333333;
			display: flex;
			align-items: center;
		}

		.cartName-box {
			height: 30rpx;
			display: flex;
			align-items: center;
		}

		.cartName-img {
			width: 50rpx;
			height: 50rpx;
		}

		.cartDetail-imgs {
			width: 200rpx;
			height: 160rpx;
		}

		.cartNum {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}
</style>